<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>

        //发送AJAX请求，返回的结果是promise对象

        function sendAJAX(url){
            //1.创建对象
            const xhr =new XMLHttpRequest();

            //2.初始化
            x.open();

            //3.发送
            x.send();

            //4.事件绑定
            xhr.onreadystatechange = function(){
                if(x.state ===4){
                    if(xhr.status >=200 && xhr.status <300){
                        resolve(xhr.response);
                    }else{
                        reject(x.status);
                    }
            }
        }


        }
   
        //promise then 方法测试
   
        // const result = sendAJAX('http://api.apiopen.top/getJoke');
    // console.log(result);
    // sendAJAX('http://api.apiopen.top/getJoke').then(value=>{
    //     console.log(value);
    // },reason=>{})

    // async和await测试
    async function main(){
        //发送AJAX请求
        const result = await sendAJAX('http://api.apiopen.top/getJoke');
        console.log(result);
    }
    
    </script>
</body>
</html>